package view;

import static java.awt.BorderLayout.CENTER;
import static java.awt.BorderLayout.NORTH;
import static java.awt.BorderLayout.SOUTH;
import static java.awt.BorderLayout.WEST;
import java.awt.BorderLayout;
import javax.swing.JFrame;

import model.Sheet;
import control.ExitListener;
import control.XLMenuBar;

public class Gui extends JFrame {
    private static final int ROWS = 10, COLUMNS = 8;
    private GuiList guiList;
    private StatusArea statusArea = new StatusArea();
    private int count;

    private Gui(GuiList guiList, int count, Sheet sheet) {
        super("Untitled-" + count);
        this.guiList = guiList;
        this.count = count;
        setLocation(count * 20, count * 20);
        guiList.add(this);
        setLayout(new BorderLayout());
        // statusPanel
        BorderPanel statusPanel = new BorderPanel();
        CurrentView currentView = new CurrentView();
        statusPanel.add(WEST, currentView);
        statusPanel.add(CENTER, statusArea);
        //editor
        Editor editor = new Editor(sheet, currentView);
        // sheetPanel
        
        BorderPanel sheetPanel = new BorderPanel();
        SheetView sheetView = new SheetView(ROWS, COLUMNS, sheet, currentView, editor);
        sheetPanel.add(WEST, new RowNumberPanel(ROWS));
        sheetPanel.add(CENTER, sheetView);
        // gui
        add(NORTH, statusPanel);
        add(CENTER, editor);
        add(SOUTH, sheetPanel);
        setJMenuBar(new XLMenuBar(this, guiList, statusArea, sheet, currentView));
        addWindowListener(new ExitListener());
        sheet.addObserver(statusArea);
        pack();
        setResizable(false);
        setVisible(true);
    }

    public Gui(Gui oldGui) {
        this(oldGui.guiList, oldGui.count + 1, new Sheet("SI"));
    }

    public void rename(String title) {
        setTitle(title);
        guiList.setChanged();
    }

    public static void main(String[] args) {
        new Gui(new GuiList(), 0, new Sheet("SI"));
    }
}